import type { PlatformHideConfig } from '../../../lib/abstract/base_hide_module'
import type { ComponentStyleIdType } from '../../../types/interfaces'
import { BaseHideModule } from '../../../lib/abstract/base_hide_module'
import {
  BAI_DU_SEARCH_PAGE_SELECTORS,
  BaiduPageType,
} from '../../../lib/page_handler/platform/baidu/types'
import {
  BING_SEARCH_PAGE_SELECTORS,
  BingPageType,
} from '../../../lib/page_handler/platform/bing/types'
import {
  GOOGLE_SEARCH_PAGE_SELECTORS,
  GooglePageType,
} from '../../../lib/page_handler/platform/google/types'
import {
  SO_SEARCH_PAGE_SELECTORS,
  SoPageType,
} from '../../../lib/page_handler/platform/so/types'
import {
  SOGOU_SEARCH_PAGE_SELECTORS,
  SogouPageType,
} from '../../../lib/page_handler/platform/sogou/types'
import { ComponentStyleId, logger, SUPPORTED_PLATFORMS } from '../../../types/constants'
import { ConfigManager } from '../../config_manager'
import { SearchDropdownType } from './style_enhancement/platforms'

interface Config {
  enabled?: boolean
}

export class HideSearchHistory extends BaseHideModule {
  readonly id: string = 'hide_search_history'
  readonly styleId: string = ComponentStyleId[this.name]
  isInitialized: boolean = false
  readonly supportedPlatforms: string[] = ConfigManager.getInstance().moduleSupportedPlatforms(this.name)
  private static instance: HideSearchHistory

  config: Config = {
    enabled: true,
  }

  get name(): ComponentStyleIdType {
    return 'HideSearchHistory'
  }

  /**
   * 获取单例实例
   */
  public static getInstance(): HideSearchHistory {
    if (!HideSearchHistory.instance) {
      HideSearchHistory.instance = new HideSearchHistory()
    }
    return HideSearchHistory.instance
  }

  /**
   * 清理操作
   */
  handleCleanupOthers(): void {
    this.applyHide(true, SearchDropdownType.History)
  }

  core(): void {
    this.loadConfig()

    if (!this.config.enabled)
      return

    this.applyHide(false, SearchDropdownType.History)
  }

  onToggleCallback(_id: string, checked: boolean): void {
    super.onToggleCallback(_id, checked)
  }

  getPlatformConfig(): PlatformHideConfig {
    return {
      [SUPPORTED_PLATFORMS.BAI_DU]: this.createPlatformConfig(
        BAI_DU_SEARCH_PAGE_SELECTORS,
        [
          BaiduPageType.Web,
          BaiduPageType.News,
          BaiduPageType.Index,
        ],
        SearchDropdownType.History,
      ),
      [SUPPORTED_PLATFORMS.GOOGLE]: this.createPlatformConfig(
        GOOGLE_SEARCH_PAGE_SELECTORS,
        [
          GooglePageType.Index,
          GooglePageType.IndexHk,
          GooglePageType.All,
          GooglePageType.Video,
          GooglePageType.ShortVideo,
          GooglePageType.Web,
        ],
        SearchDropdownType.History,
      ),
      [SUPPORTED_PLATFORMS.BING]: this.createPlatformConfig(
        BING_SEARCH_PAGE_SELECTORS,
        [
          BingPageType.Web,
          BingPageType.Video,
          BingPageType.Academic,
          BingPageType.Dictionary,
          BingPageType.Index,
        ],
        SearchDropdownType.History,
      ),
      [SUPPORTED_PLATFORMS.SO_GOU]: this.createPlatformConfig(
        SOGOU_SEARCH_PAGE_SELECTORS,
        [
          SogouPageType.Web,
          SogouPageType.Weixin,
          SogouPageType.Zhihu,
          SogouPageType.Medical,
          SogouPageType.Ask,
          SogouPageType.Encyclopedia,
          SogouPageType.News,
          SogouPageType.Knowledge,
          SogouPageType.IndexSearch,
          SogouPageType.IndexWeixin,
        ],
        SearchDropdownType.History,
      ),
      [SUPPORTED_PLATFORMS.SO]: {
        ...this.createPlatformConfig(
          SO_SEARCH_PAGE_SELECTORS,
          [
            SoPageType.Video,
            SoPageType.News,
            SoPageType.Wenku,
            SoPageType.IndexSearch,
            SoPageType.IndexVideo,
            SoPageType.IndexWenku,
          ],
          SearchDropdownType.History,
        ),
        [SoPageType.Web]: {
          styleHandler: () => this.registerSoPageStyle(),
        },
      },
    }
  }

  /**
   * 注册 360 搜索页面的样式
   */
  registerSoPageStyle(): void {
    const style: string = `
${SO_SEARCH_PAGE_SELECTORS[SoPageType.Web]!.searchBox.dropdown.history} {
  display: none !important;
  
  + #suggest-align {
    border-radius: 10px 0 0 10px !important;
  }
}
    `
    this.registerStyle(this.styleId, style)
  }

  applyConfig(config: Partial<Config>): void {
    super.updateConfig(config)

    this.applyHide(false, SearchDropdownType.History)

    logger.info(`[${this.name}] Config updated and apply`, this.config)
  }

  getStyles(): string {
    return ``
  }
}
